package com.zhonghan.recharge.service.impl;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhonghan.common.bean.Result;
import com.zhonghan.common.utils.StringUtils;
import com.zhonghan.recharge.dao.UserDao;
import com.zhonghan.recharge.pojo.User;
import com.zhonghan.recharge.service.UserService;
@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserDao userDao;
    @Override
    public long getUserRowCount(){
        return userDao.getUserRowCount();
    }
    @Override
    public List<User> selectUser(){
        return userDao.selectUser();
    }
    @Override
    public List<User> selectUserByObj(User obj){
        return userDao.selectUserByObj(obj);
    }
    @Override
    public User selectUserById(Integer id){
        return userDao.selectUserById(id);
    }
    @Override
    public int insertUser(User value){
        return userDao.insertUser(value);
    }
    @Override
    public int insertNonEmptyUser(User value){
        return userDao.insertNonEmptyUser(value);
    }
    @Override
    public int insertUserByBatch(List<User> value){
        return userDao.insertUserByBatch(value);
    }
    @Override
    public int deleteUserById(Integer id){
        return userDao.deleteUserById(id);
    }
    @Override
    public int updateUserById(User enti){
        return userDao.updateUserById(enti);
    }
    @Override
    public int updateNonEmptyUserById(User enti){
        return userDao.updateNonEmptyUserById(enti);
    }

    public UserDao getUserDao() {
        return this.userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    
	@Override
	public Result getUserList(Integer pageNum, Integer pageSize, String beginTime, String endTime, String username) {
		pageNum = pageNum == null ? 1 : pageNum;
		pageSize = pageSize == null ? 10 : pageSize;
		
		Result result = new Result();
		
		try {
			PageHelper.startPage(pageNum,pageSize);
			List<User> list = userDao.getUserList(beginTime,endTime,username);
			PageInfo<User> pageInfo = new PageInfo<>(list);
			result.setSuccess("1", pageInfo, null);
		} catch (Exception e) {
			result.setFailure("-1", "系统繁忙,请稍后再试!");
			e.printStackTrace();
		}
		
		return result;
	}
	
	@Override
	public Result addUser(String username, String password, String telephone, String nickname, String realname, Integer roleId) {
		
		Result result = new Result();
		
		User user = new User();
		
		if(StringUtils.isNotEmpty(username)){
			user.setUsername(username);
		}
		user.setTelephone(telephone);
		
		user.setNickname(nickname);
		
		user.setRealname(realname);
		
		user.setRoleId(roleId);
		
		if(StringUtils.isNotEmpty(password)){
			user.setPassword(StringUtils.md5EncryptWithSalt(password, "zhonghan"));
		}
		
		try {
			
			int code = userDao.insertNonEmptyUser(user);
			
			if(code > 0){
				result.setSuccess("1", null, "添加成功!");
			}else{
				result.setFailure("0", "添加失败!");
			}
			
		} catch (Exception e) {
			result.setFailure("-1", "系统繁忙,请稍后再试!");
			e.printStackTrace();
		}
		
		return result;
	}

}